"use strict";

function _instanceof(n, e) {
	return null != e && "undefined" != typeof Symbol && e[Symbol.hasInstance]
		? !!e[Symbol.hasInstance](n)
		: n instanceof e;
}
function _typeof(o) {
	"@babel/helpers - typeof";
	return (
		(_typeof =
			"function" == typeof Symbol && "symbol" == typeof Symbol.iterator
				? function (o) {
						return typeof o;
				  }
				: function (o) {
						return o &&
							"function" == typeof Symbol &&
							o.constructor === Symbol &&
							o !== Symbol.prototype
							? "symbol"
							: typeof o;
				  }),
		_typeof(o)
	);
}
function _classCallCheck(a, n) {
	if (!_instanceof(a, n))
		throw new TypeError("Cannot call a class as a function");
}
function _defineProperties(e, r) {
	for (var t = 0; t < r.length; t++) {
		var o = r[t];
		(o.enumerable = o.enumerable || !1),
			(o.configurable = !0),
			"value" in o && (o.writable = !0),
			Object.defineProperty(e, _toPropertyKey(o.key), o);
	}
}
function _createClass(e, r, t) {
	return (
		r && _defineProperties(e.prototype, r),
		t && _defineProperties(e, t),
		Object.defineProperty(e, "prototype", { writable: !1 }),
		e
	);
}
function _toPropertyKey(t) {
	var i = _toPrimitive(t, "string");
	return "symbol" == _typeof(i) ? i : i + "";
}
function _toPrimitive(t, r) {
	if ("object" != _typeof(t) || !t) return t;
	var e = t[Symbol.toPrimitive];
	if (void 0 !== e) {
		var i = e.call(t, r || "default");
		if ("object" != _typeof(i)) return i;
		throw new TypeError("@@toPrimitive must return a primitive value.");
	}
	return ("string" === r ? String : Number)(t);
}
var CarKeyboard = /*#__PURE__*/ (function () {
	function CarKeyboard(options) {
		_classCallCheck(this, CarKeyboard);
		if (!options.elem) {
			throw new Error("必须提供初始化参数");
		}
		if (!options.isValidated) {
			options.isValidated = false;
		}
		this.options = options;
		this.plateState = {
			currentPosition: 0,
			// 当前输入位置 (0:省份, 1:字母, 2-7:数字字母)
			plateChars: ["", "", "", "", "", "", "", ""],
			selectedProvince: "",
			keyboardVisible: false
		};
		if (this.options.elem) {
			this.init();
		}
	}
	return _createClass(CarKeyboard, [
		{
			key: "init",
			value: function init() {
				var html =
					'\n            <!-- \u8F66\u724C\u8F93\u5165\u533A\u57DF -->\n\t\t\t<div class="plate-input-container">\n\t\t\t\t<div class="plate-display" id="plateDisplay">\n\t\t\t\t\t<span class="plate-char" id="province"></span>\n\t\t\t\t\t<span class="plate-char" id="letter"></span>\n\t\t\t\t\t<span class="plate-char" id="char1"></span>\n\t\t\t\t\t<span class="plate-char" id="char2"></span>\n\t\t\t\t\t<span class="plate-char" id="char3"></span>\n\t\t\t\t\t<span class="plate-char" id="char4"></span>\n\t\t\t\t\t<span class="plate-char" id="char5"></span>\n\t\t\t\t\t<span class="plate-char last-char" id="char6"></span>\n\t\t\t\t</div>\n\t\t\t</div>\n\n\t\t\t<!-- \u865A\u62DF\u952E\u76D8 -->\n\t\t\t<div class="virtual-keyboard">\n\t\t\t\t<!-- \u5173\u95ED\u952E\u76D8\u6309\u94AE -->\n\t\t\t\t<div class="close-keyboard">\u5173\u95ED\u952E\u76D8</div>\n\n\t\t\t\t<!-- \u7701\u4EFD\u9009\u62E9\u9762\u677F -->\n\t\t\t\t<div class="keyboard-panel" id="provincesPanel">\n\t\t\t\t\t<!-- \u7701\u4EFD\u952E\u76D8\u7B2C\u4E00\u884C -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-province="\u4EAC">\u4EAC</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u6D25">\u6D25</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u5180">\u5180</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u664B">\u664B</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u8499">\u8499</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u8FBD">\u8FBD</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u5409">\u5409</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u9C81">\u9C81</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u8C6B">\u8C6B</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u7CA4">\u7CA4</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<!-- \u7701\u4EFD\u952E\u76D8\u7B2C\u4E8C\u884C -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-province="\u9ED1">\u9ED1</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u6CAA">\u6CAA</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u82CF">\u82CF</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u6D59">\u6D59</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u7696">\u7696</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u95FD">\u95FD</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u8D63">\u8D63</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u9102">\u9102</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u6E58">\u6E58</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u6842">\u6842</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<!-- \u7701\u4EFD\u952E\u76D8\u7B2C\u4E09\u884C -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-province="\u6E1D">\u6E1D</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u5DDD">\u5DDD</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u8D35">\u8D35</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u4E91">\u4E91</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u85CF">\u85CF</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u9655">\u9655</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u7518">\u7518</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u743C">\u743C</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u9752">\u9752</button>\n\t\t\t\t\t\t<button class="key-btn" data-province="\u5B81">\u5B81</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<!-- \u7701\u4EFD\u952E\u76D8\u7B2C\u56DB\u884C -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-province="\u65B0">\u65B0</button>\n\t\t\t\t\t\t<button class="key-btn delete-btn" id="toLetter">123/ABC</button>\n\t\t\t\t\t\t<button class="key-btn delete-btn" id="deleteBtnOne">\u232B</button>\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\n\t\t\t\t<!-- \u5B57\u7B26\u8F93\u5165\u9762\u677F -->\n\t\t\t\t<div class="keyboard-panel" id="charsPanel" style="display: none">\n\t\t\t\t\t<!-- \u6570\u5B57\u952E\u76D8 -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-value="0">0</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="1">1</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="2">2</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="3">3</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="4">4</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="5">5</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="6">6</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="7">7</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="8">8</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="9">9</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<!-- \u5B57\u6BCD\u952E\u76D8 -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-value="A">A</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="B">B</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="C">C</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="D">D</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="E">E</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="F">F</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="G">G</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="H">H</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="J">J</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="K">K</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-value="L">L</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="M">M</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="N">N</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="P">P</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="Q">Q</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="R">R</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="S">S</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="T">T</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="U">U</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="V">V</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn" data-value="W">W</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="X">X</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="Y">Y</button>\n\t\t\t\t\t\t<button class="key-btn" data-value="Z">Z</button>\n\t\t\t\t\t\t<button class="key-btn special" data-value="\u6E2F">\u6E2F</button>\n\t\t\t\t\t\t<button class="key-btn special" data-value="\u6FB3">\u6FB3</button>\n\t\t\t\t\t\t<button class="key-btn special" data-value="\u6302">\u6302</button>\n\t\t\t\t\t\t<button class="key-btn special" data-value="\u9886">\u9886</button>\n\t\t\t\t\t\t<button class="key-btn special" data-value="\u4F7F">\u4F7F</button>\n\t\t\t\t\t\t<button class="key-btn special" data-value="\u5B66">\u5B66</button>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<!-- \u5220\u9664\u6309\u94AE -->\n\t\t\t\t\t<div class="keyboard-row">\n\t\t\t\t\t\t<button class="key-btn delete-btn" id="toProvinces">\u7701/\u5E02</button>\n\t\t\t\t\t\t<button class="key-btn delete-btn" id="deleteBtnTwo">\u232B \u5220\u9664</button>\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t</div>\n        ';
				$("#".concat(this.options.elem)).html(html);
				this.updatePlateDisplay();
				this.bindEvent();
			}

			// 更新车牌显示
		},
		{
			key: "updatePlateDisplay",
			value: function updatePlateDisplay() {
				var self = this;
				var positions = [
					"province",
					"letter",
					"char1",
					"char2",
					"char3",
					"char4",
					"char5",
					"char6"
				];
				$.each(positions, function (index, id) {
					var $element = $("#" + id);
					$element.removeClass("active new-energy-hint");
					// 处理最后一位的特殊显示
					if (index === 7) {
						// 最后一位 char6
						if (self.plateState.plateChars[index]) {
							$element.text(self.plateState.plateChars[index]);
						} else {
							$element.text("新能源").addClass("new-energy-hint");
						}
					} else {
						$element.text(self.plateState.plateChars[index] || "");
					}
				});

				// 设置当前活动位置 - 即使全部输入完成也保持选中状态
				if (self.plateState.currentPosition < positions.length) {
					$("#" + positions[self.plateState.currentPosition]).addClass(
						"active"
					);
				}
			}

			// 输入字符
		},
		{
			key: "inputChar",
			value: function inputChar(char) {
				var self = this;
				if (
					self.plateState.currentPosition < self.plateState.plateChars.length
				) {
					self.plateState.plateChars[self.plateState.currentPosition] = char;
					// 如果不是最后一个位置，则移动到下一个位置
					if (
						self.plateState.currentPosition <
						self.plateState.plateChars.length - 1
					) {
						self.plateState.currentPosition++;
					}
					self.updatePlateDisplay();
				}
			}

			// 删除字符
		},
		{
			key: "deleteChar",
			value: function deleteChar() {
				var self = this;
				self.plateState.plateChars[self.plateState.currentPosition] = "";
				// 如果删除的是省份位置，清空省份信息
				if (self.plateState.currentPosition === 0) {
					self.plateState.selectedProvince = "";
				}
				if (
					self.plateState.currentPosition === 1 ||
					self.plateState.currentPosition === 0
				) {
					self.switchTab("provinces"); // 省份位置显示省市键盘
				} else {
					self.switchTab("chars"); // 省份位置显示字符键盘
				}

				// 如果当前位置不是第一个位置且当前位置为空，向前移动光标
				if (
					self.plateState.currentPosition > 0 &&
					self.plateState.plateChars[self.plateState.currentPosition] === ""
				) {
					// 查找前一个非空位置
					var prevPosition = self.plateState.currentPosition - 1;
					while (
						prevPosition >= 0 &&
						self.plateState.plateChars[prevPosition] === ""
					) {
						prevPosition--;
					}
					self.plateState.currentPosition = Math.max(0, prevPosition);
				}
				self.updatePlateDisplay();
			}

			// 展示键盘
		},
		{
			key: "showKeyboard",
			value: function showKeyboard() {
				var self = this;
				if (!self.plateState.keyboardVisible) {
					$("#".concat(self.options.elem))
						.find(".virtual-keyboard")
						.addClass("show");
					self.plateState.keyboardVisible = true;
					// 根据当前输入位置自动选择键盘类型
					if (self.plateState.currentPosition === 0) {
						self.switchTab("provinces"); // 省份位置显示省市键盘
					} else {
						self.switchTab("chars"); // 其他位置显示字符键盘
					}
				}
			}

			// 隐藏键盘
		},
		{
			key: "hideKeyboard",
			value: function hideKeyboard() {
				var self = this;
				if (self.plateState.keyboardVisible) {
					$("#".concat(self.options.elem))
						.find(".virtual-keyboard")
						.removeClass("show");
					self.plateState.keyboardVisible = false;
				}
			}

			// 键盘切换
		},
		{
			key: "switchTab",
			value: function switchTab(tabName) {
				var self = this;
				// 切换面板显示
				$("#".concat(self.options.elem))
					.find("#charsPanel")
					.css("display", tabName === "chars" ? "block" : "none");
				$("#".concat(self.options.elem))
					.find("#provincesPanel")
					.css("display", tabName === "provinces" ? "block" : "none");
			}

			// 绑定函数
		},
		{
			key: "bindEvent",
			value: function bindEvent() {
				var self = this;
				// 键盘事件
				$("#".concat(self.options.elem)).on("click", ".key-btn", function () {
					var value = $(this).attr("data-value");
					var province = $(this).attr("data-province");
					if (province) {
						// 省份选择
						self.plateState.selectedProvince = province;
						self.plateState.plateChars[0] = province;
						self.plateState.currentPosition = 1; // 选择省份后跳到字母位置
						self.updatePlateDisplay();
						self.switchTab("chars"); // 自动切换到字符输入
					} else if (value && !$(this).hasClass("delete-btn")) {
						// 字符输入
						self.inputChar(value);
					}
				});

				// 删除事件
				$("#".concat(self.options.elem)).on(
					"click",
					"#deleteBtnOne",
					function () {
						self.deleteChar();
					}
				);
				$("#".concat(self.options.elem)).on(
					"click",
					"#deleteBtnTwo",
					function () {
						self.deleteChar();
					}
				);

				// 省份切换
				$("#".concat(self.options.elem)).on(
					"click",
					"#toProvinces",
					function () {
						self.switchTab("provinces");
					}
				);

				// 字母切换
				$("#".concat(self.options.elem)).on("click", "#toLetter", function () {
					self.switchTab("chars");
				});

				// 点击展示键盘
				// $(`#${self.options.elem}`).on("click", "#plateDisplay", function () {
				// 	self.showKeyboard();
				// });

				// 点击第一个输入框
				$("#".concat(self.options.elem)).on("click", "#province", function () {
					self.plateState.currentPosition = 0;
					self.updatePlateDisplay();
					self.showKeyboard();
					self.switchTab("provinces"); // 显示省市键盘
				});

				// 点击后续输入框
				$("#".concat(self.options.elem)).on(
					"click",
					"#letter, #char1, #char2, #char3, #char4, #char5, #char6",
					function () {
						// 获取当前点击的输入框位置
						var positions = [
							"province",
							"letter",
							"char1",
							"char2",
							"char3",
							"char4",
							"char5",
							"char6"
						];
						var clickedId = $(this).attr("id");
						var newPosition = positions.indexOf(clickedId);
						if (newPosition !== -1) {
							self.plateState.currentPosition = newPosition;
							self.updatePlateDisplay();
							self.showKeyboard();
							self.switchTab("chars"); // 显示字符键盘
						}
					}
				);

				// 关闭键盘
				$("#".concat(self.options.elem)).on(
					"click",
					".close-keyboard",
					function () {
						self.hideKeyboard();
					}
				);
			}

			// 清空车牌
		},
		{
			key: "clearPlate",
			value: function clearPlate() {
				var self = this;
				self.plateState.plateChars = ["", "", "", "", "", "", "", ""];
				self.plateState.currentPosition = 0;
				self.plateState.selectedProvince = "";
				self.updatePlateDisplay();
				self.hideKeyboard();
			}
			// 获取车牌信息
		},
		{
			key: "getPlate",
			value: function getPlate() {
				var self = this;
				var plate = self.plateState.plateChars.join("");
				if (self.options.isValidated) {
					// 车牌号正则表达式
					// 普通车牌：粤B12345 / 粤BD12345
					var normalPlateRegex =
						/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$/;

					// 新能源车牌：粤B12345D / 粤BD12345F
					var newEnergyPlateRegex =
						/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{5}[A-Z0-9挂学警港澳]$/;
					if (plate.length < 7) {
						return {
							isValidated: false,
							plate: plate
						};
					}
					if (normalPlateRegex.test(plate) || newEnergyPlateRegex.test(plate)) {
						return {
							isValidated: true,
							plate: plate
						};
					} else {
						return {
							isValidated: false,
							plate: plate
						};
					}
				} else {
					return plate;
				}
			}
			// 回显函数
		},
		{
			key: "setPlate",
			value: function setPlate(plateNumber) {
				var self = this;
				if (!plateNumber || typeof plateNumber !== "string") {
					throw new Error("请提供有效的车牌号");
				}
				self.clearPlate();
				var chars = plateNumber.split("");
				if (chars.length < 7 || chars.length > 8) {
					throw new Error("车牌号长度不正确，应为7-8位");
				}

				// 填充状态
				for (
					var i = 0;
					i < chars.length && i < self.plateState.plateChars.length;
					i++
				) {
					self.plateState.plateChars[i] = chars[i];
				}

				// 设置省份
				if (chars.length > 0) {
					self.plateState.selectedProvince = chars[0];
				}

				// 设置当前位置为最后一个有效字符的下一位（用于继续输入）
				self.plateState.currentPosition = Math.min(
					chars.length,
					self.plateState.plateChars.length - 1
				);
				// 更新显示
				self.updatePlateDisplay();
				// 展示键盘
				self.showKeyboard();
			}

			// 销毁函数
		},
		{
			key: "destroy",
			value: function destroy() {
				var self = this;
				// 移除所有事件监听器
				$("#".concat(self.options.elem)).off();
				// 清空DOM内容
				$("#".concat(self.options.elem)).empty();
				// 重置状态
				self.plateState = {
					currentPosition: 0,
					plateChars: ["", "", "", "", "", "", "", ""],
					selectedProvince: "",
					keyboardVisible: false
				};
				// 清空选项
				self.options = null;
			}
		}
	]);
})();
